<template>
	<view class='regist'>
		<view class='content'>
			<view class='titles'>
				<view class='titleOne'>绑定手机号</view>
				<view class='titleTwo'></view>
			</view>
			<view class='forms'>
				<view class='formLine dFlex jBetween_aCenter'>
					<view class='lineTitle'>手机号</view>
					<view class='lineVal dFlex jBetween_aCenter'>
						<view class='iptBox'>
							<input type="number" v-model="tel" placeholder="请输入手机号">
						</view>
					</view>
				</view>
				<view class='formLine dFlex jBetween_aCenter'>
					<view class='lineTitle'>验证码</view>
					<view class='lineVal dFlex jBetween_aCenter'>
						<view class='iptBox'>
							<input type="number" v-model="code" placeholder="请输入验证码">
						</view>
						<text class='code' @tap='getCode'>{{codeFlag?"获取验证码":time+'s后重新发送'}}</text>
					</view>
				</view>
			</view>
			<view class='redBtn' @tap='saveTel'>绑定</view>
		</view>
	</view>
</template>

<script>
	let timer = null;
	export default {
		data() {
			return {
				tel:"",
				code:'',
				flag:false,
				codeFlag:true,
				time:60,
			};
		},
		onLoad() {
			
		},
		onShow(){
			
		},
		methods:{
			// 绑定
			saveTel(){
				if(!this.code){
					uni.showToast({
						title:"验证码不能为空",
						icon:"none"
					})
					return;
				}
				this.tool.getData("Regist/bindTel",{
					memberId:uni.getStorageSync('userId')?uni.getStorageSync('userId'):'',
					tel:this.tel,
					code:this.code,
				}).then(res=>{
					if(res){
						uni.showToast({
							title:'绑定成功',
							icon:"success"
						})
						uni.setStorageSync('userId',res.memberId);
						if(res.spreadId){
							getApp().globalData.userId = res.memberId;
						}
						
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1650)
						
					}else{
						this.codeFlag = true;
						this.time = 60;
					}
				}).catch(err=>{
					console.log(err);
				})
			},
			// 获取验证码
			getCode(){
				if(!this.tel){
					uni.showToast({
						title:"请输入手机号码",
						icon:"none"
					})
					return;
				}
				if(this.codeFlag){
					this.tool.getData("Regist/sendSmsMessage",{
						tel:this.tel,
						token:'aadac7f36499f233352f79f599ee3606'
					}).then(res=>{
						console.log(res);
						if(res){
							this.codeFlag = false;
							let time = 60;
							timer = setInterval(()=>{
								time -- ;
								if(time>0){
									this.time = time;
								}else{
									this.time = 60;
									this.codeFlag = true;
									clearInterval(timer);
								}
							},1000)
						}
					}).catch(err=>{
						console.log(err);
					})
				}else{
					uni.showToast({
						title:"请不要频繁操作",
						icon:"none"
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	.regist{
		width: 100%;
		overflow: auto;
		.content{
			width: 80%;
			margin: 15% auto 0;
			.redBtn{
				width: 100%;
				background:#4886FF;
				padding:10px 0;
				color:white;
				text-align:center;
				border-radius: 5px;
			}
			.xieyi{
				width: 100%;
				margin-top: 28upx;
				color: #999999;
				font-size: 12px;
				.xieyiStatusIcon{
					width: 30upx;
					height:30upx;
					margin-right: 10upx;
				}
				text{
					color: #4886FF;
				}
			}
			.regist{
				font-size:12px;
				margin-top: 30upx;
				text-align: center;
			}
			.forms{
				width: 100%;
				margin-bottom: 60upx;
				.formLine{
					width: 100%;
					margin-bottom: 40upx;
					background: #e6e6e6;
					padding:22upx 26upx;
					box-sizing: border-box; 
					border-radius: 8upx;
					.lineTitle{
						width: 25%;
						margin-right: 15upx; 
					}
					.lineVal{
						flex: 1;
						.iptBox{
							flex: 1;
							margin-right:20upx;
						}
						.code{
							width: auto;
							color: #4886FF;
						}
					} 
				}
			}
			.titles{
				width: 100%;
				margin-bottom:100upx;
				.titleOne{
					font-size: 24px;
				}
				.titleTwo{
					color: #999999;
					font-size: 15px;
					margin-top: 20upx;
				}
			}
		}
	}
	page{
		background:#f6f6f6;
	}
</style>